// pages/morejob/certify/certify.js
Page({
  data: {
    region: "",
    ageRange: {},
    job: {},
    wantTask: "0",
    wxNumbers: {},
    qqNumbers: {},

    taskList: [],
    currentTaskTmp: {},
    currentTask: {
      id: "0",
      name: "全部",
    },
    wechatPic: [{
      url: "https://www.linxiaobang.com.cn/miniprogram/image/wxfriend.jpg",
    }],
    qqPic: [{
      url: "https://www.linxiaobang.com.cn/miniprogram/image/qqfriend.jpg"
    }],
    wechatFriendPic: [],
    wechatFriendPicId: "",
    qqFriendPic: [],
    qqFriendPicId: "",
    isDialog_completeInfo: true,
    ageList_show: false,
    ageList: [],
    jobList_show: false,
    jobList: [],
    WCList_show: false,
    WCList: [],
    QQList_show: false,
    QQList: [],
    isDialog_choosetask: false,
    beforeClose: null
  },
  onLoad: function (options) {
    wx.enableAlertBeforeUnload({
      message: "确定关闭吗?完善的信息有助于接到更多、更愿意做的派单任务，请谨慎考虑~"
    });
    let _this = this;
    _this.setData({
      beforeClose: (action) => new Promise((resolve) => {
        if (action === "confirm") {
          _this.taskConfirm()
        } else {
          console.log("重置")
          resolve(false);
        }
      })
    });
    let obj = {
      taskList: wx.getStorageSync("taskList"),
      ageList: wx.getStorageSync("ageList"),
      jobList: wx.getStorageSync("jobList"),
      WCList: wx.getStorageSync("WCList"),
      QQList: wx.getStorageSync("QQList")
    }
    obj.ageList.splice(0, 1);
    obj.jobList.splice(0, 1);
    obj.WCList.splice(0, 1);
    obj.QQList.splice(0, 1);
    _this.setData({
      taskList: obj.taskList,
      ageList: obj.ageList,
      jobList: obj.jobList,
      WCList: obj.WCList,
      QQList: obj.QQList
    });
    if (options.takeOrderAuth == 1) {
      getApp().globalData.http.takeOrderAuthInfoInfo().then(res => {
        this.setData({
          region: res.data.region,
          ageRange: this.getCurrentItem(this.data.ageList, res.data.ageRange),
          job: this.getCurrentItem(this.data.jobList, res.data.job),
          wantTask: res.data.wantTask,
          currentTask: this.getCurrentItem(this.data.taskList, res.data.wantTask),
          wxNumbers: this.getCurrentItem(this.data.WCList, res.data.wxNumbers),
          qqNumbers: this.getCurrentItem(this.data.QQList, res.data.qqNumbers),
          wechatFriendPic: !res.data.wxFriendImg ? [] : [{
            url: res.data.wxFriendImg
          }],
          wechatFriendPicId: res.data.wxFriendImgId,
          qqFriendPic: !res.data.qqFriendImg ? [] : [{
            url: res.data.qqFriendImg
          }],
          qqFriendPicId: res.data.qqFriendImgId
        });
      });
    }
  },
  onShow: function () {
    let city = wx.getStorageSync("city_select");
    if (!!city) {
      this.setData({
        region: wx.getStorageSync("city_select")
      });
    }
  },
  getCurrentItem(list, id) {
    for (let i = 0; i < list.length; i++) {
      if (list[i].id == id) {
        return (list[i])
      }
    }
  },
  taskSel(e) {
    let tmp = {
      id: e.currentTarget.dataset.obj.id,
      name: e.currentTarget.dataset.obj.name,
    }
    this.setData({
      currentTaskTmp: tmp
    });
  },
  taskConfirm() {
    this.setData({
      currentTask: this.data.currentTaskTmp,
      isDialog_choosetask: false
    });
  },
  completeInfo() {
    this.setData({
      isDialog_completeInfo: true
    });
  },
  cityHandle() {
    wx.navigateTo({
      url: "/pages/morejob/cityselect/cityselect",
    })
  },
  actionHandle(e) {
    let prop = e.currentTarget.dataset.showname;
    this.setData({
      [prop]: true
    });
  },
  actionClose(e) {
    let prop = e.currentTarget.dataset.showname;
    this.setData({
      [prop]: false
    });
  },
  actionSel(e) {
    let prop = e.currentTarget.dataset.showname;
    let type = e.currentTarget.dataset.type;
    this.setData({
      [prop]: false,
      [type]: e.detail
    });
  },
  taskHandle() {
    this.setData({
      currentTaskTmp: this.data.currentTask,
      isDialog_choosetask: true
    });
  },
  submit() {
    let params = {
      region: this.data.region,
      ageRange: this.data.ageRange.id,
      job: this.data.job.id,
      wantTask: this.data.currentTask.id,
      // wxNumbers: this.data.wxNumbers.id,
      // qqNumbers: this.data.qqNumbers.id,
      // wxFriendImgId: this.data.wechatFriendPicId,
      // qqFriendImgId: this.data.qqFriendPicId
    };
    if (!params.region) {
      getApp().globalData.vant.toast("请选择区域");
    } else if (!params.ageRange && params.ageRange != 0) {
      getApp().globalData.vant.toast("请选择年龄");
    } else if (!params.job && params.job != 0) {
      getApp().globalData.vant.toast("请选择职业");
    } else if (!params.wantTask && params.wantTask != 0) {
      getApp().globalData.vant.toast("请选择意向任务");
    } else {
      getApp().globalData.http.takeOrderAuth(params).then(res => {
        getApp().globalData.vant.toast({
          type: "success",
          message: "操作成功",
          duration: 1000,
          onClose: () => {
            wx.disableAlertBeforeUnload();
            wx.navigateBack();
          },
        });
      });
    }
  },
  uploadWXFri(e) {
    console.log(e)
    let filePath = e.detail.file.url;
    this.setData({
      wechatFriendPic: [{
        url: filePath,
        status: "uploading",
        message: "上传中"
      }]
    });
    getApp().globalData.http.upload("name", filePath, {
      srcType: 1
    }).then(res => {
      let tmp = this.data.wechatFriendPic;
      tmp[0].status = "success";
      this.setData({
        wechatFriendPic: tmp,
        wechatFriendPicId: res.data.id
      });
    });
  },
  deleteWXFri() {
    this.setData({
      wechatFriendPic: [],
    });
  },
  uploadQQFri(e) {
    let filePath = e.detail.file.url;
    this.setData({
      qqFriendPic: [{
        url: filePath,
        status: "uploading",
        message: "上传中"
      }]
    });
    getApp().globalData.http.upload("name", filePath, {
      srcType: 1
    }).then(res => {
      let tmp = this.data.qqFriendPic;
      tmp[0].status = "success";
      this.setData({
        qqFriendPic: tmp,
        qqFriendPicId: res.data.id
      });
    });
  },
  deleteQQFri() {
    this.setData({
      qqFriendPic: [],
    });
  },
})